package speed.mvc.action;

import java.util.Date;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.velocity.context.Context;

import speed.context.SpeedContext;
import speed.mvc.servlet.RequestUtils;

import com.google.inject.Injector;

/**
 * @author zhaohaolin(zhaohaolin2010@gmail.com)
 */
public abstract class BaseAction implements IBaseAction {

	public void setVcontext(Context vcontext) {
		this.vctx = vcontext;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
		this.session = this.request.getSession();
		this.cookie = this.request.getCookies();
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public interface Model {
		public void add(String name, Object value);
	}

	public class ViewModel implements Model {

		@Override
		public void add(String name, Object value) {
			vctx.put(name, value);
		}

	}

	// Guice IOC
	public void setSpeedContext(SpeedContext context) {
		this.context = context;
		this.injector = context.getInjector();
	}

	protected SpeedContext context;
	protected Injector injector;

	// 得到服务层实例
	@SuppressWarnings("unchecked")
	protected <T> T getService(Class<?> service) {
		return (T) this.injector.getInstance(service);
	}

	// 得到Repository实例
	@SuppressWarnings("unchecked")
	protected <T> T getRepository(Class<?> repository) {
		return (T) this.injector.getInstance(repository);
	}

	// 得到Mapper实例
	@SuppressWarnings("unchecked")
	protected <T> T getMapper(Class<?> repository) {
		return (T) this.injector.getInstance(repository);
	}

	protected String getByRequest(String name) {
		return this.request.getParameter(name);
	}

	protected String get(String name) {
		return this.request.getParameter(name);
	}

	protected void set(String name, Object obj) {
		this.request.setAttribute(name, obj);
	}

	protected void add(String name, Object obj) {
		this.model.add(name, obj);
	}

	protected void addModel(String name, Object obj) {
		this.model.add(name, obj);
	}

	protected void setAttribute(String name, Object obj) {
		this.request.setAttribute(name, obj);
	}

	protected Object getAttribute(String name) {
		return this.request.getAttribute(name);
	}

	protected Object session(String name) {
		return this.session.getAttribute(name);
	}

	protected Object getSession(String name) {
		return this.session.getAttribute(name);
	}

	protected String getWebRoot() {
		return this.request.getContextPath();
	}

	protected String getEncoding() {
		return this.request.getCharacterEncoding();
	}

	protected int getInt(String _name, int _default) {
		return RequestUtils.getInt(this.request, _name, _default);
	}

	protected double getDouble(String _name, double _default) {
		return RequestUtils.getDouble(this.request, _name, _default);
	}

	protected long getLong(String _name, long _default) {
		return RequestUtils.getLong(this.request, _name, _default);
	}

	protected float getFloat(String _name, float _default) {
		return RequestUtils.getFloat(this.request, _name, _default);
	}

	protected boolean getBoolean(String _name, boolean _default) {
		return RequestUtils.getBoolean(this.request, _name, _default);
	}

	protected Date getDate(String _name, Date _default) {
		return RequestUtils.getDate(this.request, _name, _default);
	}

	protected String getString(String _name) {
		return RequestUtils.getString(this.request, _name);
	}

	protected boolean hasParameter(String _name) {
		return this.request.getParameter(_name) != null;
	}

	protected String[] getParameterValues(String _name) {
		return this.request.getParameterValues(_name);
	}

	protected Enumeration<String> getParameterNames() {
		return this.request.getParameterNames();
	}

	protected Map<String, String[]> getParameterMap() {
		return this.request.getParameterMap();
	}

	protected String forward(String url) {
		return url;
	}

	protected String redirect(String url) {
		return "redirect:" + url;
	}

	public Model model = new ViewModel();
	protected Context vctx;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;
	protected Cookie[] cookie;
	protected Logger logger = Logger.getLogger(getClass());

}
